package com.hyf.translucentstatusbar.base;

import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;

import com.hyf.translucentstatusbar.R;
import com.hyf.translucentstatusbar.widget.ScrollListenerScrollView;

/**
 * Created by Administrator on 2017/9/11.
 */

public abstract class BaseActivity extends BaseAbstractActivity implements OnScrollListener {

    private RelativeLayout rootView;

    private int slidingDistance;

    @Override
    public void setContentView(View view) {
        super.setContentView(view);
        rootView = (RelativeLayout) findViewById(R.id.hyf_container);
        if (isImageHeader()) {
            setHeaderBarAlpha(0);
        } else {
            // 设置margin
            int headerHeight = toolBar.getLayoutParams().height + getStatusBarHeight();
            ViewGroup.MarginLayoutParams params = (ViewGroup.MarginLayoutParams) rootView.getLayoutParams();
            params.setMargins(0, headerHeight, 0, 0);
            rootView.setLayoutParams(params);
            setHeaderBarAlpha(255);
        }
    }

    @Override
    public final void onScrollChange(int scrollY) {
        if (slidingDistance <= 0) return;
        if (scrollY < 0) {
            scrollY = 0;
        }
        float alpha = (float) scrollY / (float) slidingDistance;
        if (scrollY <= slidingDistance) {
            setHeaderBarAlpha((int) (255 * alpha));
        } else {
            setHeaderBarAlpha(255);
        }
        onScrollChange(scrollY, alpha);
    }

    protected void onScrollChange(int scrollY, float alpha) {

    }

    protected final void setHeaderBarScrollChange(ScrollListenerScrollView scrollView, int viewHeight) {
        // 首先判断是否是imageHeader
        if (!isImageHeader())
            return;
        // 其次判断viewHeight的高度是否大于headerbar的高度
        slidingDistance = viewHeight - (toolBar.getLayoutParams().height + getStatusBarHeight());
        if (viewHeight <= 0)
            return;
        // 最后设置事件
        scrollView.setScrollListener(this);
    }

    protected abstract boolean isImageHeader();
}
